package com.etc.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.dao.DiscussMapper;
import com.etc.dao.ListenworldMapper;
import com.etc.entity.Listenworld;
import com.etc.entity.ListenworldExample;
import com.etc.entity.ListenworldExample.Criteria;
import com.etc.entity.ListenworldExt;
import com.etc.entity.Result;
import com.etc.service.ListenworldService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ListenworldServiceImpl implements ListenworldService {
	
	@Autowired
	private ListenworldMapper listenworldMapper;
	
	@Autowired
	private DiscussMapper discussMapper;
	@Override
	public ListenworldExt findAllListenworld(Integer page,Integer catId) {
		//分页
		PageHelper.startPage(page, 10);
		//正常查询
		List<Listenworld> worlds= listenworldMapper.selectByCatId(catId);
		//得到分页后的结果
		PageInfo<Listenworld> pageInfo = new PageInfo<>(worlds);
		List<Listenworld> list = pageInfo.getList();
		//创建一个Listenworld的包装类
		ListenworldExt ext = new ListenworldExt();
		ext.setQings(list);
		//设置响应状态
		ext.setRet(1);
		//设置下一页数
		ext.setPage(page + 1);
		//设置信息
		ext.setMsg("成功");
		//设置总页数
		ext.setPageNum(pageInfo.getPages());
		return ext;
	}

	@Override
	public ListenworldExt updateLcount(Integer lid, Integer lcount) {
		ListenworldExt ext = new ListenworldExt();
		Listenworld listenworld = new Listenworld();
		listenworld.setLid(lid);
		listenworld.setLcount(lcount);
		listenworldMapper.updateByPrimaryKeySelective(listenworld);
		ext.setMsg("修改成功！");
		return ext;
	}

	@Override
	public int updateListenworld(Integer lid) {
		//根据id得到一个Listenworld对象
		Listenworld world = listenworldMapper.selectByPrimaryKey(lid);
		world.setLpageview(world.getLpageview() + 1);
		//修改浏览量,浏览量加1
		return listenworldMapper.updateByPrimaryKeySelective(world);
	}

	@Override
	public Result findListenWorldAndUser(Integer lid,Integer uid) {
		Listenworld world = listenworldMapper.findListenWorldAndUser(lid);
		//日期转换
		Date date = world.getLcreatetime();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
		//创建返回类型
		Result result = new Result();
		result.setWorld(world);
		result.setFormatDate(dateFormat.format(date));
		//通过该用户id查找用户的评论量
		/*if(uid == null || uid == 0) {
			uid = 1;
		}*/
		int discussCount = discussMapper.findDiscussCount(uid);
		//该用户的评论量
		result.setDiscussCount(discussCount);
		//设置响应码为1正常
		result.setStatus(1);
		return result;
	}

	@Override
	public Result findAllImgsByPage(Integer page,Integer pageSize) {
		if(page == null || page == 0) {
			page = 1;
			pageSize = 20;
		}
		PageHelper.startPage(page, pageSize);
		List<ListenworldExt> worlds = listenworldMapper.findAllImgsByPage();
		PageInfo<ListenworldExt> pageInfo = new PageInfo<>(worlds);
		//得到分页后的结果
		List<ListenworldExt> list = pageInfo.getList();
		//创建一个返回的结果集
		Result result = new Result();
		for (ListenworldExt ext : list) {
			if(ext.getLstatus() == 1) {
				ext.setCheckStatus("已审核");
			}else {
				ext.setCheckStatus("未审核");
			}
		}
		//设置有多少条记录
		result.setCounts((int)pageInfo.getTotal());
		result.setList(list);
		//设置当前页
		result.setPageNum(pageInfo.getPageNum());
		//设置总页数
		result.setPages(pageInfo.getPages());
		return result;
	}

	@Override
	public int updateStatus(Integer lid, Integer lstatus) {
		Listenworld world = new Listenworld();
		world.setLid(lid);
		if(lstatus == 1) {
			world.setLstatus(0);
		}else {
			world.setLstatus(1);
		}
		return listenworldMapper.updateByPrimaryKeySelective(world);
	}

	
}
